<template>
  <TestLayout>
    <template v-slot:main-left>
      <Institution />
    </template>
    <template v-slot:main-button>
      <mainButton
        :main-button="mainButton"
        @toMainSearch="search"
        @toMainSearchReset="reset"
      />
    </template>
    <template v-slot:search-info>
      <float-search
        offset-direction="left"
        class="float-search"
        @click="isBox = !isBox"
      />
      <searchMain
        ref="searchMain"
        size="mini"
        :label-width="labelWidth"
        :model="formData"
        :rules="searchRules"
        :item-list="formItemList"
      />
      <el-divider />
    </template>
    <template v-slot:main>
      <drawer-layout>
        <template v-slot:table-left>
          <common-table
            :data="infoList"
            border
            :columns-item="columns"
            :title="infoTitle"
            :loading="false"
            :item-list="itemList"
            :display-column="displayColumn"
            @checkbox-change="selectChangeEvent"
          />
        </template>
        <template v-slot:table-right>
          <common-table
            :data="testList"
            border
            :columns-item="testColumns"
            :title="testTitle"
            :display-column="displayColumnText"
            @checkbox-change="selectChangeEvent"
          />
        </template>
      </drawer-layout>
    </template>
    <!-- 弹框 -->
    <template v-slot:add-dialog>
      <!-- <pageDialog
        ref="pageDialog"
        :dialog-visible.sync="dialog_show"
        :dialog-config.sync="dialogConfig"
      /> -->
      <adaptive-dialog
        v-if="dialog_test_show"
        key="test"
        :visible.sync="dialog_test_show"
        :test-dialog-config.sync="testDialogConfig"
        title="增加家庭档案"
      >
        <template v-slot:default>
          <DynamicLayout @get-lost-page="getLostPage">
            <AddFamilyForm ref="ruleForm" />
          </DynamicLayout>
        </template>
        <template v-slot:footer>
          <span>
            <el-button type="primary" @click="submit('testForm')"
              >新增成员</el-button
            >
            <el-button type="success" @click="previewFamily">预 览</el-button>
            <el-button type="info">打 印</el-button>
            <el-button type="warning">导 出</el-button>
            <el-button @click="addDialogClosed">取 消</el-button>
            <el-button
              v-show="isSubmit"
              type="primary"
              @click="submit('testForm')"
              >保 存</el-button
            >
          </span>
        </template>
      </adaptive-dialog>
      <adaptive-dialog
        v-if="dialog_personage_show"
        key="test1"
        :visible.sync="dialog_personage_show"
        :test-dialog-config.sync="personageDialogConfig"
        title="新增个人档案"
      >
        <template v-slot:default>
          <DynamicLayout @get-lost-page="getPersonageLostPage">
            <AddRecordForm ref="ruleForm" />
          </DynamicLayout>
        </template>
        <template v-slot:footer>
          <span>
            <el-button type="success" @click="previewPersonage"
              >预 览</el-button
            >
            <el-button type="success">打 印</el-button>
            <el-button type="success">导 出</el-button>
            <el-button @click="addPersonageClosed">取 消</el-button>
            <el-button
              v-show="isPersonageSubmit"
              type="primary"
              @click="submit('testForm')"
              >确 定</el-button
            >
          </span>
        </template>
      </adaptive-dialog>
    </template>
  </TestLayout>
</template>
<script>
import TestLayout from "@/components/Layout/test.vue"
import Institution from "@/components/Trees/Institution.vue"
import mainButton from "@/components/CommonButtons/MainButton.vue"
import searchMain from "@/components/CommonForm/index.vue"
import CommonTable from "@/components/CommonTable/index.vue"
import DrawerLayout from "@/components/DrawerLayout/index.vue"
// import pageDialog from "@/views/dialog/record/familyFilesDialog.vue"
// import addDialogTest from "./dialog/addDialogTest.vue"
import FloatSearch from "@/components/FloatSearch/index.vue"
// import DynamicDialogLayout from "@/components/Layout/dynamicDialog.vue"
import AdaptiveDialog from "@/components/Dialog/AdaptiveDialog.vue"
import DynamicLayout from "@/components/Layout/dynamicLayout.vue"
import AddFamilyForm from "./dialog/AddFamilyForm.vue"
import AddRecordForm from "@/views/record/personal-record/dialog/AddRecordForm.vue"
// 加载模拟数据
import {
  infoList,
  testList,
  sexs,
  fileStatusType,
  liveType,
  dataSource,
  helpPoor,
  peopleSigns,
  publicSigns,
  addressList,
  dataProps,
  searchHandle
} from "@/simdata/healthData.js"
export default {
  name: "Roles",
  components: {
    TestLayout,
    Institution,
    mainButton,
    searchMain,
    CommonTable,
    DrawerLayout,
    // pageDialog,
    // addDialogTest,
    FloatSearch,
    // DynamicDialogLayout,
    AdaptiveDialog,
    DynamicLayout,
    AddFamilyForm,
    AddRecordForm
  },
  data() {
    return {
      isSubmit: false, // 控制确定提交显示
      isPersonageSubmit: false,
      itemList: [
        {
          label: "贫",
          color: "#BF924C"
        }
      ],
      // 配置常用按钮
      mainButton: [
        {
          label: "新增家庭",
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          handle: () => this.addHandle()
        },
        {
          label: "导出Excel",
          icon: "el-icon-edit-outline",
          type: "success",
          handle: () => {
            console.log("点击了")
          }
        },
        {
          label: "档案调用",
          icon: "el-icon-postcard",
          type: "warning",
          handle: () => {
            console.log("点击档案了")
          }
        },
        {
          label: "新增成员",
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          handle: () => this.addTestHandler()
        }
      ],
      // 查询表单
      formData: {
        Address_value: [], // 常用地址
        keyword: "", // 个人信息项 字段
        keyValue: "", // 值
        createDate: null, // 建档日期 开始
        endDate: null // 结束日期
      },
      // 验证
      searchRules: {
        Address_value: [
          { required: true, message: "请选择常用地址", trigger: "change" }
        ]
      },
      // label的宽度
      labelWidth: "auto",
      // 配置search label表单
      formItemList: [
        {
          type: "myInformation",
          label: "个人信息查询项目",
          span: 16,
          options: [
            { label: "家庭档案号", value: 2 },
            { label: "姓名", value: 3 },
            { label: "身份证号", value: 4 }
          ],
          props: dataProps
        },
        {
          type: "TimeQueryTest",
          label: "建档日期",
          prop: "createDate",
          width: "80px",
          span: 8,
          placeholder: "请选择..."
        }
      ],
      isBox: false,
      // 所有列表数据
      infoTitle: "家庭基本信息列表",
      testTitle: "家庭成员列表",
      infoList: infoList,
      testList: [
        {
          id: "001",
          name: "张浩",
          sex: 2,
          age: "26",
          birthdata: "1995-10-05",
          identityNumber: "421081199510051866",
          relation: "父子"
        }
      ],
      // 配置显示的label标签
      columns: [
        { prop: "name", label: "户主姓名", width: "100" },
        { prop: "identityNumber", label: "户主姓名", width: "100" },
        { prop: "phonenumber", label: "本人电话", width: "100" },
        { prop: "detailedAddress", label: "门牌号码", width: "100" },
        { prop: "nowAddress", label: "家庭地址", width: "100" },
        { prop: "createDate", label: "建档日期", width: "100" },
        { prop: "number", label: "家庭档案号", width: "100" }
      ],
      // 控制表格操作按钮的显示隐藏
      displayColumn: {
        operationCol: true,
        editBtn: true,
        removeBtn: true
      },
      // 家庭成员按钮显示与隐藏
      displayColumnText: {
        operationCol: true,
        editBtn: true,
        removeBtn: true
      },
      // 新增字段
      testModel: {
        name: "",
        sex: null,
        age: "",
        birthdata: null,
        identityNumber: "",
        relation: ""
      },
      // test配置label标签
      testColumns: [
        {
          type: "Input",
          prop: "name",
          label: "姓名",
          width: "100",
          span: 12,
          placeholder: "请输入姓名"
        },
        {
          type: "Select",
          prop: "sex",
          label: "性别",
          width: "68",
          span: 12,
          placeholder: "请选择性别",
          option: [
            {
              value: 1,
              label: "未知的性别"
            },
            {
              value: 2,
              label: "男"
            },
            {
              value: 3,
              label: "女"
            },
            {
              value: 4,
              label: "全部"
            }
          ]
        },
        {
          type: "Input",
          prop: "age",
          label: "年龄",
          width: "80",
          span: 12,
          placeholder: "请输入年龄"
        },
        {
          type: "data",
          prop: "birthdata",
          label: "出生日期",
          width: "100",
          span: 12,
          placeholder: "请选择..."
        },
        {
          type: "Input",
          prop: "identityNumber",
          label: "身份证号",
          width: "180",
          span: 12,
          placeholder: "输入身份证号"
        },
        {
          type: "Input",
          prop: "relation",
          label: "与户主关系",
          width: "100",
          span: 12,
          placeholder: "输入与户主关系"
        }
      ],
      // test表格验证字段
      testRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        birthdata: [
          { required: true, message: "请选择出生日期", trigger: "blur" }
        ],
        identityNumber: [
          { required: true, message: "请输入正确的身份证号码", trigger: "blur" }
        ],
        relation: [
          { required: true, message: "请输入与户主的关系", trigger: "blur" }
        ]
      },
      testId: 100, // 模拟的id
      // 弹框的显示隐藏
      dialog_show: false,
      dialog_test_show: false,

      dialog_personage_show: false,
      // 表格左侧配置
      dialogConfig: {
        title: "",
        dialogType: "new" // 新增的控制
      },
      // 表格右侧配置
      testDialogConfig: {
        title: "",
        dialogType: "new" // 新增的控制
      },
      // 表格右侧配置
      personageDialogConfig: {
        title: "",
        dialogType: "new" // 新增的控制
      }
    }
  },
  created() {},

  methods: {
    previewPersonage() {
      const routeUrl = this.$router.resolve({
        path: "/preview/personal-record"
      })
      window.open(routeUrl.href, "_blank")
    },
    previewFamily() {
      const routeUrl = this.$router.resolve({
        path: "/preview/family-files"
      })
      window.open(routeUrl.href, "_blank")
    },
    // 点击新增按钮
    addHandle() {
      console.log("点击新增了")
      // this.dialog_show = true
      this.dialog_test_show = true
    },
    // 点击新增成员按钮
    addTestHandler() {
      this.dialog_test_show = true
    },
    // 点击搜索按钮
    search(formName) {
      this.$refs.searchMain.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.formData)

          // 调用接口
          this.callbackComponent()
        } else {
          console.log("error submit!!")
          return false
        }
      })
    },
    // 点击重置按钮
    reset(formName) {
      this.$refs.searchMain.$refs[formName].resetFields()
      // console.log(this.formData)
    },
    // 关闭弹框
    addDialogClosed() {
      // 重置表单
      this.$refs.ruleForm.resetForm()
      this.dialog_test_show = false
    },
    addPersonageClosed() {
      // 重置表单
      this.dialog_personage_show = false
    },
    // 点击确定
    submit() {
      this.$confirm("是否保存后，添加个人档案", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.dialog_test_show = false
          this.dialog_personage_show = true
        })
        .catch(() => {})
    },
    // 点击搜索后，请求后台接口了
    callbackComponent() {
      console.log("调接口")
    },

    // 家庭基本信息复选框选中
    selectChangeEvent({ checked, records }) {
      console.log(checked)
      console.log(records)
    },

    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
    },
    // 监听 页码值的改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
    },
    // 点击新增档案
    addRoles() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        // 新增
        if (this.dialogType === "new") {
          const formData = {
            ROW_ID: this.testId++,
            XM: this.form.XM,
            ZJHM: this.form.ZJHM,
            YLJGMC: this.form.YLJGMC,
            XB: this.form.XB,
            DHHM: this.form.DHHM,
            GRDAID: this.form.GRDAID,
            recordTime: this.form.recordTime,
            JZDZ: this.form.JZDZ,
            type: this.form.type
          }
          this.HealthTableData.unshift(formData)
        } else {
          // 修改
          this.HealthTableData.some(item => {
            const isFind = item.ROW_ID === this.form.ROW_ID
            if (isFind) {
              // console.log(item)
              Object.assign(item, this.form)
            }
            return isFind
          })
        }
        this.$message.success("操作成功!")
        // // 隐藏添加用户的对话框
        this.addDialogVisible = false
        this.$refs.addFormRef.resetFields()
      })
    },
    handleBtn() {
      this.dialogType = "new"
      this.addDialogVisible = true
    },
    // 点击修改
    handleEdit(data) {
      // console.log(data)
      this.dialogType = data.dialogType
      this.addDialogVisible = true
      this.$nextTick(() => {
        Object.assign(this.form, data.scope.row)
      })
    },
    // 点击删除
    handleRemove(index) {
      // console.log(index)
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.HealthTableData.splice(index, 1)
          this.$message({
            type: "success",
            message: "删除成功!"
          })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          })
        })
    },
    // 建档日期控件
    dateChangeRecordTime(val) {
      console.log(val)
      this.form.recordTime = val
    },

    // 获取所有角色的列表
    async getRolesList() {
      const { data: res } = await this.$http.get("roles")
      if (res.meta.status !== 200) {
        return this.$message.error("获取角色列表失败")
      }
      console.log(res)
      this.rolelist = res.data
    },

    // 展示分配权限的对话框
    async showSetRightDialog(role) {
      this.roleId = role.id
      // 获取所有分配权限的数据
      const { data: res } = await this.$http.get("rights/tree")
      if (res.meta.status !== 200) {
        return this.$message.error("获取权限数据失败！")
      }
      // 获取的数据赋值到列表
      this.rightslist = res.data
      // 递归获取三级节点的id
      this.grtLeafKeys(role, this.defKeys)
      this.setRightDialogVisible = true
    },
    // 通过递归，获取角色下的所有三级权限的id，保存到defKeys
    grtLeafKeys(node, arr) {
      // 如果当前 node 节点不包含children属性，则是三级节点
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.grtLeafKeys(item, arr)
      })
    },
    // 监听分配权限对话框的关闭事件
    setRightDialogClosed() {
      // this.defKeys = []
    },
    // 显示确定提交
    getLostPage(isType) {
      this.isSubmit = isType
    },
    // 显示确定提交
    getPersonageLostPage(isType) {
      this.isPersonageSubmit = isType
    }
  }
}
</script>
<style lang="scss" scoped></style>
